package com.cauli.file.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cauli.file.component.FileComponent;
import com.cauli.model.entity.BaseEntity;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Cauli
 * @date 2023-03-18 12:00
 * @description 用户文件
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("user_file")
@ApiModel(value = "UserFile", description = "用户文件")
public class UserFile extends BaseEntity {
    /**
     * 用户文件关联ID
     */
    @JsonSerialize(using = ToStringSerializer.class)
    @TableId("user_file_id")
    private Long userFileId;

    /**
     * 用户ID
     */
    @JsonSerialize(using = ToStringSerializer.class)
    @TableField("user_id")
    private Long userId;

    /**
     * 文件ID
     */
    @JsonSerialize(using = ToStringSerializer.class)
    @TableField("file_id")
    private Long fileId;

    /**
     * 文件名
     */
    @TableField("file_name")
    private String fileName;

    /**
     * 扩展名
     */
    @TableField("extend_name")
    private String extendName;

    /**
     * 文件路径
     */
    @TableField("file_path")
    private String filePath;

    /**
     * 是否是目录(0否 1是)
     */
    @TableField("is_dir")
    private Integer isDir;

    /**
     * 删除时间
     */
    @TableField("delete_time")
    private String deleteTime;

    /**
     * 删除批次号
     */
    @TableField("delete_batch_num")
    private String deleteBatchNum;

    /**
     * 回收站删除标志(0存在 1删除)
     */
    @TableField("delete_flag")
    private Integer deleteFlag;

    public UserFile() {
    }

    public UserFile(FileComponent fileComponent, Long userId, Long fileId) {
        this.userId = userId;
        this.fileId = fileId;
        this.filePath = fileComponent.getParent();
        this.fileName = fileComponent.getNameNotExtend();
        this.extendName = fileComponent.getExtendName();
        this.isDir = fileComponent.isDirectory() ? 1 : 0;
        this.deleteFlag = 0;
    }

    public boolean isDirectory() {
        return this.isDir == 1;
    }

    public boolean isFile() {
        return this.isDir == 0;
    }
}
